package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 李旭
 * @Time: 11:26
 * @Description: 全文检索开始
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    //ES 客户端  高级 性能高
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    //ES 客户端 低级   性能差
 /*   @Autowired
    private TransportClient client;*/


    //SpringDataES  高级
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    //SpringDataES  低级
/*    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;*/

    @Autowired
    private ListService listService;


    //创建索引  包含Mapping
    @GetMapping("/createIndex")
    public Result createIndex() {
        //1：创建索引  2： type 3: Mapping
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        //ES                Mysql
        //index              gmall_product
        //type               sku_info
        //Mapping 映射        id int(11) ,name varchar（20）
        //  Feild 域           字段
        return Result.ok();
    }
    //导入索引   根据skuId
    @GetMapping("/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        listService.upperGoods(skuId);
        return Result.ok();
    }
    //删除索引 根据skuId
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok();
    }
    //增加热度 评分
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }

    //开始搜索ES索引库
    @PostMapping("/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){
        //高级客户端 ES官方
        //SpringDataEs
        SearchResponseVo vo = listService.search(searchParam);
        return vo;
    }


}
